package sfsoftware.util.input;

import java.util.Vector;

import org.lwjgl.input.Keyboard;

public class Kboard {		
	public static int[] leftKeys = 	{Keyboard.KEY_A, Keyboard.KEY_LEFT};
	public static int[] rightKeys = {Keyboard.KEY_D, Keyboard.KEY_RIGHT};
	public static int[] upKeys = 	{Keyboard.KEY_W, Keyboard.KEY_UP};
	public static int[] downKeys = 	{Keyboard.KEY_S, Keyboard.KEY_DOWN};	
	public static int[] pauseKeys = {Keyboard.KEY_TAB, Keyboard.KEY_RSHIFT};
	
	private static Vector<Integer> lastKeyState = new Vector<Integer>();
	
	/**
	 * Accepts a key and, if pressed, returns true
	 * 
	 * @param key
	 * @return
	 */
	public static boolean isKeyDown(int key)
	{		
		return Keyboard.isKeyDown(key);
	}
	
	/**
	 * Accepts an array of keys and, if any are currently pressed, returns true
	 * 
	 * @param keyGroup
	 * @return
	 */
	public static boolean isKeyDown(int[] keyGroup)
	{
		for(int i = 0; i < keyGroup.length; i++)
		{
			if(isKeyDown(keyGroup[i]))
			{
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * Returns true if the given key is pressed and wasn't just pressed in the last pass.
	 * Ideal for a single keystroke.
	 * 
	 * @param key
	 * @return
	 */
	public static boolean keyPressed(int key)
	{		
		return (isKeyDown(key) && !(lastKeyState.contains(key)));
	}
	
	public static boolean keyPressed(int[] keyGroup)
	{
		for(int i = 0; i < keyGroup.length; i++)
		{
			if(keyPressed(keyGroup[i]))
			{
				return true;
			}
		}
		return false;
	}
	
	public static String getKeyName(int key)
	{
		String result =	Keyboard.getKeyName(key);
		if(key == Keyboard.KEY_LCONTROL)
		{
			result = "L Ctrl";
		}
		else if(key == Keyboard.KEY_RCONTROL)
		{
			result = "R Ctrl";
		}		
		result = result.toUpperCase();
		return result;
	}
	
	/**
	 * Stores the currently pressed keys
	 */
	public static void storeKBState()
	{
		lastKeyState.clear();
		for(int i = 0; i < Keyboard.KEYBOARD_SIZE; i++)
		{
			if(Keyboard.isKeyDown(i))
			{		
				lastKeyState.add(i);
			}
		}
	}
	
}
